<?php
namespace App\Admin\Controllers\Basics;

use App\Models\Basics\AmazonAccount;
use App\Models\Basics\AmazonCountry;
use App\Models\Basics\SysCountry;
use App\Models\Basics\Currency;
use App\Models\Operation\ProfitSku;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class AmazonCountryController extends AdminController{

    protected $title = '亚马逊店铺国家';

    protected function grid()
    {
        $grid = new Grid(new AmazonCountry());
        $grid->column('id','ID')->style('width: 5%');
        $grid->column('country.country','国家');
        $grid->column('country.country_cn','名称');
        $grid->column('aws_region','区域')->display(function ($value){
            return AmazonAccount::$_awsRegion[$value];
        });
        $grid->column('marketplace_id','商城ID');
        $grid->column('seller_central_url','商城URL');
        $grid->column('url_order','URL排序')->editable();
        $grid->column('fan_eu','是否泛欧')->using(AmazonCountry::$_fanEu)->label([1 => 'info', 2 => 'warning', 0 => 'default']);
        $grid->column('currency.code','币种');
        $grid->column('vat','VAT')->display(function ($value){
            return $value.'%';
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->equal('country_id', '国家')->select(AmazonCountry::options());
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('aws_region','区域')->select(AmazonAccount::$_awsRegion);
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('marketplace_id', '商城ID');
            });
        });
        return $grid;
    }

    public function form()
    {
        $form = new Form(new AmazonCountry());

        $form->hidden('id');

        $form->select('country_id', '国家')
            ->options(SysCountry::options())
            ->creationRules(['required', "unique:b_amazon_country"])
            ->updateRules(['required', "unique:b_amazon_country,country_id,{{id}}"])->setWidth(5);
        $form->select('aws_region','区域')->options(AmazonAccount::$_awsRegion)->required()->setWidth(5);
        $form->text('marketplace_id', '商城ID')->required()->setWidth(5);
        $form->text('seller_central_url', '商城URL')->required()->setWidth(5);
        $form->number('url_order', 'URL排序')->min(1)->required()->setWidth(5);
        $form->radio('fan_eu','泛欧')
            ->options(AmazonCountry::$_fanEu)
            ->default(0)
            ->required()
            ->help('若该国家属欧洲站点请务必选择是否泛欧（默认非欧）')->setWidth(5);
        $form->select('currency_code','币种')->options(Currency::options())
            ->required()
            ->help('若币种不存在请先添加')->setWidth(5);
        $form->rate('vat','VAT')->default('0.000')->required()->setWidth(5);
        $form->saving(function (Form $form) {
            if ($form->marketplace_id && $form->model()->marketplace_id != $form->marketplace_id) {
                $form->marketplace_id = strtoupper($form->marketplace_id);
            }
            if ($form->vat && $form->model()->vat != $form->vat) {
                ProfitSku::query()
                    ->where('country_id',$form->model()->country_id)
                    ->update(['vat' => $form->vat]);
            }
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
